package com.ruihang.generalibrary.ui.dialog;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.utils.GeneraLifeObser;

/**
 * Created by Administrator on 2017/2/13.
 */

public abstract class BaseBottomSheetFrag extends BottomSheetDialogFragment {
    protected Context mContext;

    protected View rootView;
    protected BottomSheetBehavior mBehavior;

    protected Dialog dialog;

    protected int peekHeight = -1;
    protected ProgressDialog progressDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_ATTACH);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED_V);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_RESUME);
    }

    @Override
    public void onPause() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_STOP);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY_V);
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DETACH);
        GeneraLifeObser.removeLife(this);
        super.onDetach();
    }

    @Override
    public void onStart() {
        super.onStart();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_START);
        mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    @Override
    public void onDestroy() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY);
        super.onDestroy();
        //解除缓存View和当前ViewGroup的关联
        ((ViewGroup) (rootView.getParent())).removeView(rootView);
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //每次打开都调用该方法 类似于onCreateView 用于返回一个Dialog实例
        dialog = creatBottomSheetDialog(savedInstanceState);
        if (rootView == null) {
            //缓存下来的View 当为空时才需要初始化 并缓存
            rootView = View.inflate(mContext, getLayoutResId(), null);
            initView();
        }
        resetView();
        //设置View重新关联
        dialog.setContentView(rootView);
        mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
        mBehavior.setHideable(true);
        //圆角边的关键
        ((View) rootView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int width = rootView.getWidth();
                int height = rootView.getHeight();
                JLog.e("root w=" + width + "  h=" + height);
                if (width > 0 && height > 0) {
                    rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    onRootViewGlobalLayout(width, height);
                }
            }
        });
//        rootView.post(new Runnable() {
//            @Override
//            public void run() {
//                /**
//                 * PeekHeight默认高度256dp 会在该高度上悬浮
//                 * 设置等于view的高 就不会卡住
//                 */
//                mBehavior.setPeekHeight(getPeekHight());
//            }
//        });
        return dialog;
    }

    protected BottomSheetDialog creatBottomSheetDialog(Bundle savedInstanceState) {
        return (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
//        return new BottomSheetDialog(getContext(), R.style.DemandBottomSheetStyle);
    }

    protected void onRootViewGlobalLayout(final int rootWith, final int rootHeght) {
    }

    protected int getPeekHight() {
        if (peekHeight > 0) {
            return peekHeight;
        }
        return rootView.getHeight();
    }

    public String getOwnerName() {
        return this + "";
    }

    public abstract int getLayoutResId();

    /**
     * 初始化View和设置数据等操作的方法
     */
    public abstract void initView();

    /**
     * 重置的View和数据的空方法 子类可以选择实现
     * 为避免多次inflate 父类缓存rootView
     * 所以不会每次打开都调用{@link #initView()}方法
     * 但是每次都会调用该方法 给子类能够重置View和数据
     */
    public void resetView() {

    }

    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    /**
     * 使用关闭弹框 是否使用动画可选
     * 使用动画 同时切换界面Aty会卡顿 建议直接关闭
     *
     * @param isAnimation
     */
    public void close(boolean isAnimation) {
        if (isAnimation && mBehavior != null) {
            mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            rootView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            }, 200);
        } else {
            dismiss();
        }
    }

    public BaseBottomSheetFrag setPeekHeight(int peekHeight) {
        this.peekHeight = peekHeight;
        return this;
    }

    protected void toast(String str) {
        if (TextUtils.isEmpty(str) || getContext() == null) {
            return;
        }
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    protected void toast(@StringRes int str) {
//        if(TextUtils.isEmpty(str)){
//            return;
//        }
        if (getContext() == null) {
            return;
        }
        try {
            Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void closeProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    protected void showProgress(String msg) {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(msg);
        progressDialog.show();
    }

    protected void removeClick(View... vs) {
        if (vs != null && vs.length > 0) {
            for (int i = 0; i < vs.length; i++) {
                removeClick(vs[i]);
            }
        }
    }

    protected void removeClick(View v) {
        if (v != null) {
            v.setOnClickListener(null);
        }
    }
}
